package com.hfjyz.base.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hfjyz.base.bean.MenuBean;
import com.hfjyz.base.entity.MenuEntity;

public class MenuUtil {
    public static List<MenuBean> getMenuInfo(List<MenuEntity> menuList) {
        List<MenuBean> list = new ArrayList<MenuBean>();
        Map<Integer, MenuBean> map = new HashMap<Integer, MenuBean>();

        for (MenuEntity menu : menuList) {
            MenuBean bean = generatorMenu(menu);
            map.put(bean.getId(), bean);
            
        }

        for (MenuEntity menu : menuList) {
            if (menu.getParentId() == null) {
                list.add(map.get(menu.getId()));
            } else {
                MenuBean parentBean = map.get(menu.getParentId());
                parentBean.getMenu().add(map.get(menu.getId()));
                
            }
        }

        return list;
    }

    private static MenuBean generatorMenu(MenuEntity entity) {
        MenuBean bean = new MenuBean();
        bean.setId(entity.getId());
        bean.setText(entity.getMenuText());
        bean.setUrl(entity.getMenuLink());

        return bean;
    }
}
